package com.eastfair.user.service.impl;

import com.eastfair.core.context.ContextUtil;
import com.eastfair.uid.baidu.rule.SnowFlakeRole;
import com.eastfair.user.dao.UserInfoMapper;
import com.eastfair.user.entity.User;
import com.eastfair.user.entity.UserInfo;
import com.eastfair.user.service.UserInfoService;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.database.mybatis.conditions.Wraps;
import com.eastfair.database.mybatis.conditions.query.QueryWrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;

import java.util.Collection;

import org.springframework.aop.framework.AopContext;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.annotation.annotation.cache.Cache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

import com.eastfair.user.vo.*;
import com.eastfair.user.dto.*;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 业务实现类
 * 用户表详情
 * </p>
 *
 * @author ligang
 * @date 2023-09-29
 */
@Slf4j
@Service
@Transactional
public class UserInfoServiceImpl extends SuperServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Autowired
    SnowFlakeRole snowFlakeRole;
    private static Integer dbSuffix = 5;
    private static Integer tableSuffix = 2;

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(UserInfo model) {
        model.setId(snowFlakeRole.findUidOfUser(model.getUserId(), dbSuffix, tableSuffix));
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        //雪花ID
        return R.successDef();
    }


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<UserInfo> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    /**
     * 通用查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    public List<UserInfo> list(UserInfoDTO modelDTO, List<Long> ids) {
        return ((UserInfoService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
            magic = @Magic(
                    key = "{" +
                            "{'headImg', #args[0]?.headImg}," +
                            "{'nickName', #args[0]?.nickName}," +
                            "{'name', #args[0]?.name}," +
                            "{'status', #args[0]?.status}," +
                            "{'isTourist', #args[0]?.isTourist}," +
                            "{'isEnabled', #args[0]?.isEnabled}," +
                            "{'id', #args[1]}" +
                            "}"))
    public List<UserInfo> listOfCache(UserInfoDTO modelDTO, List<Long> ids) {
        //查询
        UserInfo queryModel = new UserInfo();
        if (modelDTO != null) {
            BeanUtils.copyProperties(modelDTO, queryModel);
        }

        QueryWrap<UserInfo> wrapper = Wraps.q(queryModel);
        if (ids != null && !ids.isEmpty()) {
            wrapper.lambda().in(UserInfo::getId, ids);
        }
        //添加其他业务查询条件
        wrapper.lambda().eq(UserInfo::getIsDeleted, BusinessConstant.DELETE_NO);
        List<UserInfo> modelList = list(wrapper);
        return modelList;
    }

    @Override
    public void executetest(UserInfo userInfo) {
        save(userInfo);
//        int i = 0;
//        int b = 1 / i;
    }


    /**
     * do转vo
     *
     * @param model model
     * @return modelVo
     */
    @Override
    public UserInfoVO doToVo(UserInfo model) {
        if (model == null) {
            return null;
        }
        UserInfoVO modelVo = new UserInfoVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }

    /**
     * do转vo
     *
     * @param modelList modelList
     * @return list
     */
    @Override
    public List<UserInfoVO> doToVos(List<UserInfo> modelList) {
        if (modelList == null || modelList.isEmpty()) {
            return null;
        }
        List<UserInfoVO> result = new ArrayList<>();
        for (UserInfo model : modelList) {
            result.add(doToVo(model));
        }
        return result;
    }
}
